'use strict';

angular.module('mdNote.controllers', ['ngRoute','ngSanitize','ngAnimate','mgcrea.ngStrap','ui.codemirror'])

.config(function($navbarProvider) {
    angular.extend($navbarProvider.defaults, {
        activeClass: 'in'
    });
})

// 初始菜单
.run(['$location', '$rootScope','$route', function($location, $rootScope,$route) {
    $rootScope.$on('$routeChangeSuccess', function (currentRoute, previousRoute) {
    	$rootScope.title = $route.current.title;
    });
}])

// 头部
.controller('HeaderCtrl', ['$scope','$rootScope','$location', function ($scope,$rootScope,$location) {

    // Math.round(new Date().getTime()/1000);
    $scope.today = new Date();

    $scope.isActive = function (viewLocation) { 
        return viewLocation === $location.path();
    };

}])

// 使用js读取
.controller('JsRdCtrl', ['$scope', function($scope){
    $scope.src = 'readme.md';
}])

// 关于页面
.controller('AboutCtrl', ['$scope', function($scope){
    $scope.src = 'about.md';
}])

// 读取文件
.controller('MdCtrl', ['$scope','$routeParams','MdReader','$rootScope','$location','$aside',function($scope,$routeParams,MdReader,$rootScope,$location,$aside){
    if ($location.path() === "/md") {
        $scope.path = $routeParams.m;
    }else{
        $scope.path = $location.path();
    };
    $scope.md = MdReader.one($scope.path);
    $scope.sourcePath = '/editor?m='+$location.path();

    // $('img').each(function(index, el) {
    //     $(this).attr('src',$(this).attr('src'));
    // });

    $scope.md.$promise.then(function (result) {
        $rootScope.title = result.title;
    });

    $scope.asideBar = function(){
        // TODO: cache
        $scope.menus = treeMenu();
        var myAside = $aside({
            title: '<i class=" fa fa-lemon-o"></i> ' + $rootScope.title
            ,scope: $scope
            ,template: 'views/tpl-md-menus.html'
            ,show: true
            ,html:true
            ,backdrop:true
            ,container:'#nav-header'
        });
    }

    $scope.gotoNode = function(id){
        $('html,body').animate({scrollTop: $('#'+id).offset().top}, 500);
    }

}])

// 菜单
.controller('MenusCtrl', ['$scope','Menus','$location', function($scope,Menus,$location){
    if ($location.path() != "/menus") {
        $scope.menus = Menus.find($location.path());
        $scope.baseUrl = $location.path()+'/';
    }else{
        $scope.menus = Menus.all();
    };

    $scope.destroyCache = function(){
        Menus.destroy();
    }
}])

.controller('CreateCtrl', ['$scope','MdReader', function ($scope,MdReader) {

    $scope.editorOptions = {
        lineWrapping : true,
        lineNumbers: true,
        styleActiveLine: true,
        mode: 'gfm',
        theme: "default"
    };

    $scope.mdRfresh = false;

    $scope.save = function(){
        MdReader.save($scope.md).$promise.then(function(data){
            $scope.saveStatus = data.status;
        }, function(err){
            console.log(err);
        });
    };

}])

// 编辑
.controller('EditorCtrl', ['$scope','$routeParams','MdReader','$aside','$timeout', function ($scope,$routeParams,MdReader,$aside,$timeout) {
    
    $scope.editorClass = "col-md-12";
    $scope.mdClass = "col-md-6";
    $scope.hideMd = true;
    $scope.hideMdIcon = "left";

    $scope.editorOptions = {
        lineWrapping : true,
        lineNumbers: true,
        styleActiveLine: true,
        mode: 'gfm',
        theme: "default"
    };
    $scope.md = {mdpath:"",mdc:""};

    //$location.$$search.m
    if ($routeParams.m) {
        $scope.backurl = $routeParams.m;
        var one = MdReader.one($routeParams.m);
        $scope.md.mdpath = $routeParams.m;
        one.$promise.then(function (result) {
            $scope.title = result.title;
            $scope.md.mdc = result.mdContent;
        });
    };

    $scope.asideBar = function(){
        // TODO: cache
        $scope.menus = treeMenu();
        var myAside = $aside({
            title: '<i class=" fa fa-lemon-o"></i> ' + $scope.title
            ,scope: $scope
            ,template: 'views/md_menus.html'
            ,show: true
            ,html:true
            ,backdrop:true
            ,container:'#nav-header'
        });
    }

    $scope.gotoNode = function(id){
        $('html,body').animate({scrollTop: $('#'+id).offset().top}, 500);
    }

    marked.setOptions({
      gfm: true,
      tables: true,
      highlight: function (code) {
        return hljs.highlightAuto(code).value;
      }
    });

    $scope.hideMdToggle = function(){
        if ($scope.hideMdIcon == "right") {
            $scope.hideMdIcon = "left";
            $scope.hideMd = true;
            $scope.editorClass = "col-md-12";
            // $timeout.cancel(timer);
        }else{
            $scope.hideMdIcon = "right";
            $scope.hideMd = false;
            $scope.editorClass = "col-md-6";
            var timer = $timeout(function(){console.log($scope.md); $scope.mdHtml = marked($scope.md);},1000);
        };
    }


    $scope.save = function(){
        MdReader.update($scope.md).$promise.then(function(data){
            $scope.saveStatus = data.status;
        }, function(err){
            console.log(err);
        });
    };


}])

;